package dao.impl;

import dao.UserDao;
import model.User;
import util.DBUtils;

import java.sql.*;

public class UserDaoImpl implements UserDao {
    @Override
    public User findByUserNameAndPassWord(String name,String pass) {

        String sql="select * from user where username = ? and password = ?";
        User user = new User();

        try(Connection conn = DBUtils.getConn();
            PreparedStatement prep = conn.prepareStatement(sql);
        ){
            prep.setString(1,name);
            prep.setString(2,pass);

            try(ResultSet rs = prep.executeQuery()){
                while (rs.next()){
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setName(rs.getString("name"));
                    user.setRole(rs.getString("role"));
                    user.setEmail(rs.getString("email"));
                    user.setPhone(rs.getString("phone"));
                    user.setCreate_time(rs.getTimestamp("create_time").toLocalDateTime());
                    user.setStatus(rs.getInt("status"));
                }

                return user;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public boolean userSignup() {
        return false;
    }

    @Override
    public User findByusername() {
        return null;
    }
}

